package cn.edu.jxau.test;

import java.util.Objects;

/**
 * 求链表的长度,考虑有环的情况
 * 
 * @author Fudashi
 */
public class Main {

	public static void main(String[] args) {

		Node node0 = new Node(0);
		Node node1 = new Node(1);
		Node node2 = new Node(2);
		Node node3 = new Node(3);
		Node node4 = new Node(4);
		Node node5 = new Node(5);
		Node node6 = new Node(6);
		Node node7 = new Node(7);
		Node node8 = new Node(8);
		Node node9 = new Node(8);
		Node node10 = new Node(10);
		Node node11 = new Node(11);
		Node node12 = new Node(12);
		Node node13 = new Node(13);
		node0.next = node1;
		node1.next = node2;
		node2.next = node3;
		node3.next = node4;
		node4.next = node0;
		System.out.println(Joseph(node0, 3));
	}

	public static int Joseph(Node node, int x) {

		Objects.requireNonNull(node, "参数node不能为null");
		Node cur = node;
		while (cur.next != cur) {
			for (int i = 0; i < x - 1; i++) {
				cur = cur.next;
			}
			System.out.println("del:" + cur.data);
			cur.data = cur.next.data;
			cur.next = cur.next.next;
		}
		return cur.data;
	}
}

class Node {

	public int data;
	public Node next;

	public Node() {

	}

	public Node(int data) {
		this.data = data;
	}
}